package com.foxmail.yusp75.myexam;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;


public class SingleSelectFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private OnFragmentInteractionListener mListener;

    private String c_anwser;

    RadioGroup radioGroup;

    public SingleSelectFragment() {
        // Required empty public constructor
    }

    public static SingleSelectFragment newInstance(String param1, String param2) {
        SingleSelectFragment fragment = new SingleSelectFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_single_select, container, false);

        // ui
        radioGroup = view.findViewById(R.id.single_rg);
        final RadioButton button_a = view.findViewById(R.id.single_A);
        final RadioButton button_b = view.findViewById(R.id.single_B);
        final RadioButton button_c = view.findViewById(R.id.single_C);
        final RadioButton button_d = view.findViewById(R.id.single_D);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                if (i == button_a.getId())
                    c_anwser = "A";
                if (i == button_b.getId())
                    c_anwser = "B";
                if (i == button_c.getId())
                    c_anwser = "C";
                if (i == button_d.getId())
                    c_anwser = "D";

                if (mListener != null)
                    mListener.onFragmentInteraction(c_anwser);
            }
        });

        return view;
    }


    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public void reset() {
        if (radioGroup != null)
            radioGroup.clearCheck();
    }


    public void setmListener(OnFragmentInteractionListener listener) {
        mListener = listener;
    }

    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(String c);
    }
}
